//
//  YTChangePhoneViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/22.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTChangePhoneViewModel.h"
#import "YTUserProfileServices.h"
#import <CocoaSecurity/CocoaSecurity.h>

@interface YTChangePhoneViewModel ()

@property (strong, nonatomic, readwrite) RACCommand *completeCommand;

@end

@implementation YTChangePhoneViewModel

- (void)initialize {
    [super initialize];
    
    RACSignal *completeEnable = [RACSignal combineLatest:@[self.phoneValidate, self.verifyValidate]
                                                  reduce:^id(NSNumber *phone, NSNumber *verify) {
        return @(phone.boolValue && verify.boolValue);
    }];
    
    @weakify(self);
    self.completeCommand = [[RACCommand alloc] initWithEnabled:completeEnable signalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSString *code = [self.areaCode substringFromIndex:2]; // + 86...
        // 加密
        YTProfileModel *model = [YTUtilityManager manager].userModel;
        NSString *oldMD5 = [NSString stringWithFormat:@"%@%@%@%@", self.oldVerifyCode, model.areaCode, model.phone, model.userId];
        NSString *nowMD5 = [NSString stringWithFormat:@"%@%@%@%@", self.verifyCode, code, self.phoneNum, model.userId];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"oldPaySum"] = [CocoaSecurity md5:oldMD5].hex;
        dict[@"newAreaCode"] = code;
        dict[@"newMobile"] = self.phoneNum;
        dict[@"newPaySum"] = [CocoaSecurity md5:nowMD5].hex;
        return [YTUserProfileServices changePhoneNumWithData:dict];
    }];
}

@end
